package com.xyy.saas.payment.adpater.abchinaLoan.support;

import com.abchina.openbank.OpenBankHttpRequest;
import com.abchina.openbank.common.Contants;
import com.xyy.saas.payment.adpater.abchinaLoan.config.AbchinaLoanConfig;
import com.xyy.saas.payment.common.exception.PayBizException;
import com.xyy.saas.payment.cores.enums.ResultCodeEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wangzeqi
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class AbchinaLoanClient {

    private final AbchinaLoanConfig abchinaLoanConfig;
    private static final String contentType = "application/json";

    /**
     * 贷款申请单H5
     */
    public String applyH5(String jsonParam) {
        // log.info("AbchinaClientService applyH5 jsonParam:{}", jsonParam);
        try {
            OpenBankHttpRequest request = creatRequest(abchinaLoanConfig.getRequestUrl().getApplyH5(), jsonParam);
            String h5Url = request.getReturnRequestStringH5();
            //log.info("AbchinaClientService applyH5 jsonParam:{}, h5Url:{}", jsonParam, h5Url);
            return h5Url;
        } catch (Exception e) {
            log.error("AbchinaClientService applyH5 error. jsonParam:{}", jsonParam, e);
            throw new PayBizException(ResultCodeEnum.ABCHINA_REQUEST_ERROR);
        }
    }

    /**
     * 通用请求
     */
    public String sendAndRecv(String logScene, String url, String jsonParam) {
        try {
            OpenBankHttpRequest request = creatRequest(url, jsonParam);
            String response = request.sendAndRecv();
            log.info("{} AbchinaClientService sendAndRecv response:{}, url:{}, jsonParam:{}", logScene, response, url, jsonParam);
            return response;
        } catch (Exception e) {
            log.error("{} AbchinaClientService sendAndRecv error. url:{}, jsonParam:{}", logScene, url, jsonParam, e);
            throw new PayBizException(ResultCodeEnum.ABCHINA_REQUEST_ERROR);
        }
    }

    private OpenBankHttpRequest creatRequest(String url, String jsonParam) {
        OpenBankHttpRequest request = new OpenBankHttpRequest();
        request.setSignType(Contants.SHA256);
        request.setBizData(jsonParam);
        request.setRequestUrl(url);
        request.setTimeout(abchinaLoanConfig.getClient().getTimeout());
        request.setHeader(fillRequestHeader());
        try {
            request.generateRequestString();
        } catch (Exception e) {
            log.error("AbchinaClientService creatRequest error. url:{}, jsonParam:{}", url, jsonParam, e);
            throw new PayBizException("农行请求初始化异常");
        }
        return request;
    }

    private Map<String, Object> fillRequestHeader() {
        return new HashMap<String, Object>() {
            private static final long serialVersionUID = 1977560854429201831L;

            {
                put("Content-type", contentType);
            }
        };
    }

}
